package com.wys.mall.service.component;

import com.wys.common.utils.HttpUtils;
import lombok.Data;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * 短信模版
 * @Package com.wys.mall.service.component
 * @Author charlin
 * @Version: v1.0
 * @Date 2021-07-0513:56
 * @Copyright: 2021 www.wyscha.com Inc. All rights reserved.
 */
@ConfigurationProperties(prefix = "alibaba.cloud.sms")
@Data
@Component
public class SmsComponent {

    private String host;
    private String path;
    private String templateId;
    private String appcode;

    public String sendCode(String phone, String code){
//        String host = "https://jmsms.market.alicloudapi.com";
//        String path = "/sms/send";
        String method = "POST";
//        String appcode = "ad1c2dea04914d1d8305f2359e9fba3b";
        Map<String, String> headers = new HashMap<String, String>();
        //最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("mobile", phone);
        querys.put("templateId", templateId);
//        querys.put("templateId", "M72CB42894");
        querys.put("value", code);
        Map<String, String> bodys = new HashMap<String, String>();
        try {
            HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
            return (EntityUtils.toString(response.getEntity()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
